package com.ss.android.crash.log;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.bytedance.frameworks.core.thread.TTPriority$Priority;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;

public final class k {
    private static final Object f;
    private static final FilenameFilter i;

    static {
        f = new Object();
        i = new l();
    }

    volatile Context a;
    com.ss.android.crash.log.j.j$a b;
    SharedPreferences c;
    volatile boolean d;
    private volatile String e;
    private Properties g;
    private volatile String h;
    private String j;

    k(Context arg5, com.ss.android.crash.log.j.j$a arg6) {
        String v1 = null;

        this.j = v1;
        if (arg6 != null && arg5 != null) {
            this.b = arg6;
            this.a = arg5.getApplicationContext();
            this.c = this.a.getSharedPreferences("app_crash_copy", 0);
            this.e = this.c.getString("header", v1);
            try {
                this.g = new Properties();
                this.g.load(arg5.getApplicationContext().getAssets().open("ss.properties"));
                Object v0_1 = this.a("release_build");
                if (!(v0_1 instanceof String)) {
                    String v0_2 = v1;
                } else {
                }

                this.h = ((String) v0_1);
            } catch (Throwable v0) {
                v0.printStackTrace();
            }

            com.bytedance.frameworks.core.thread.c.a();
            com.bytedance.frameworks.core.thread.c.b(new m(this, TTPriority$Priority.HIGHT));
            return;
        }

        throw new IllegalArgumentException("context and ICommonParams must not be null");
    }

    static Object a() {
        return k.f;
    }

    static void a(k arg5) throws IOException {
        String v0_3;
        Object v4;
        File v3;
        int v1;
        try {
            if (!CrashUtils.d(arg5.a)) {
                return;
            }

            File v0_1 = new File(CrashUtils.b(arg5.a), "tt_crash_log_dir");
            if (!v0_1.exists()) {
                return;
            }

            File[] v2 = v0_1.listFiles();
            Arrays.sort(((Object[]) v2), Collections.reverseOrder());
            v1 = 0;
            while (true) {
                if (v1 >= v2.length) {
                    return;
                }

                v3 = v2[v1];
                if (v1 >= 5) {
                } else {
                    v4 = k.f;
                    synchronized (v4) {
                        v0_3 = android.support.design.a.y(v3.getAbsolutePath());
                    }
                    CrashUtils.a(v0_3, com.ss.android.crash.log.j.j$a.a());
                }
                v3.delete();
                ++v1;
                continue;
            }
        } catch (Throwable v0) {

        }
    }

    static void b(k arg22) {
        Object v2_5;
        JSONObject v2_4;
        Object v9_1;
        long v4_1;
        String v3_3;
        int v9;
        BufferedReader v6_1;
        String v4;
        StringBuffer v16;
        long v10;
        int v7;
        File v15;
        BufferedReader v2_1;
        int v8;
        int v3;
        File[] v12;
        Closeable v6 = null;
        try {
            if (!CrashUtils.d(arg22.a)) {
                CrashUtils.a((Closeable) null);
                return;
            }
            v12 = new File(CrashUtils.b(arg22.a), "ss_native_android_crash_logs").listFiles(k.i);
            if (v12 == null || v12.length <= 0) {
                CrashUtils.a((Closeable) null);
                return;
            }
            Arrays.sort(((Object[]) v12), Collections.reverseOrder());
            String v13 = arg22.j;
            arg22.j = v12[0].getName();
            int v14 = v12.length;
            v3 = 0;
            v8 = 0;
            v2_1 = ((BufferedReader) v6);
            try {
                while (true) {
                    if (v8 < v14) {
                        v15 = v12[v8];
                        if (v8 < 5) {
                            if (v13 != null && (v13.equals(v15.getName()))) {
                                v7 = 1;
                            } else {
                                v7 = v3;
                            }
                        } else {
                            v7 = 1;
                        }
                    } else {
                        CrashUtils.a(((Closeable) v2_1));
                        return;
                    }

                    v10 = 0;
                    v16 = new StringBuffer();
                    v4 = null;
                    if (v7 != 0) {
                        v6_1 = v2_1;
                    } else if (v15.length() >= 16384) {
                        v6_1 = v2_1;
                    } else {
                        v6_1 = new BufferedReader(new FileReader(v15));
                        v9 = 0;
                        v3_3 = v4;
                        v4_1 = v10;
                        while (true) {
                            String v2_3 = v6_1.readLine();
                            if (v2_3 == null) {
                                break;
                            }

                            if (v9 == 0) {
                                v4_1 = Long.parseLong(v2_3);
                                v2_3 = v3_3;
                            } else if (v9 != 1) {
                                v16.append(v2_3 + "\n");
                                v2_3 = v3_3;
                            }

                            ++v9;
                            v3_3 = v2_3;
                        }
                        v6_1.close();
                        v6 = null;
                        v9_1 = null;
                        label_86:
                        if (arg22.e != null) {
                            v2_4 = new JSONObject(arg22.e);
                        } else {
                            Map v10_1 = com.ss.android.crash.log.j.j$a.a();
                            Iterator v11 = v10_1.keySet().iterator();
                            while (true) {
                                if (v11.hasNext()) {
                                    v2_5 = v11.next();
                                    ((JSONObject) null).put(((String) v2_5), v10_1.get(v2_5));
                                    continue;
                                } else {
                                    v2_4 = (JSONObject) v9_1;
                                    break;
                                }
                            }
                        }
                        JSONObject v9_2 = new JSONObject();
                        v9_2.put("header", v2_4);
                        v9_2.put("data", v16.toString().trim());
                        v9_2.put("is_native_crash", 1);
                        if (!v3_3.startsWith("no_process_name")) {
                            v9_2.put("process_name", v3_3);
                        }

                        if (v4_1 > 0) {
                            v9_2.put("crash_time", v4_1);
                        }

                        if (v3_3 == null || !v3_3.contains(":")) {
                            v9_2.put("remote_process", 0);
                        } else {
                            v9_2.put("remote_process", 1);
                        }

                        CrashUtils.a(v9_2.toString(), com.ss.android.crash.log.j.j$a.a());
                    }

                    label_123:
                    v15.delete();
                    ++v8;
                    v3 = v7;
                    v2_1 = ((BufferedReader) v6);
                }
            } catch (Exception v2_2) {

            } finally {

            }
        } catch (Throwable v2) {

        }

    }

    static com.ss.android.crash.log.j.j$a c(k arg1) {
        return arg1.b;
    }

    final String a(String arg7, String arg8) throws IOException {
        FileOutputStream v1_2 = null;
        File v3;
        String v0 = null;
        if (!TextUtils.isEmpty(((CharSequence) arg7)) && !TextUtils.isEmpty(((CharSequence) arg8))) {
            Object v2 = k.f;
            synchronized (v2) {
                try {
                    File v1 = new File(CrashUtils.b(this.a), "tt_crash_log_dir");
                    if (!v1.exists()) {
                        v1.mkdirs();
                    }

                    v3 = new File(v1, arg8);
                    if (!TextUtils.isEmpty(arg7)) {
                        v1_2 = new FileOutputStream(v3, true);
                        ((OutputStream) v1_2).write((arg7 + "\n").getBytes());
                        v0 = v3.getAbsolutePath();
                        return v0;
                    }

                    v0 = v3.getAbsolutePath();
                    return v0;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                } finally {
                    try {
                        v1_2.close();
                    } catch (Exception ex) {

                    }
                }
            }
        }
        return null;
    }

    final void a(JSONObject arg7, Context arg8) {
        int v2_3;
        String v2_1;
        int v0 = 12;
        int v1 = -12;
        try {
            v2_1 = arg8.getPackageName();
            arg7.put("package", arg8.getPackageName());
            PackageInfo v2_2 = arg8.getPackageManager().getPackageInfo(v2_1, 0);
            if (v2_2.applicationInfo == null) {

            } else {
                v2_3 = v2_2.applicationInfo.labelRes;
                if (v2_3 <= 0) {

                } else {
                    arg7.put("display_name", arg8.getString(v2_3));
                }
            }
        } catch (Exception v2) {
        }

        try {
            if (!TextUtils.isEmpty(this.h)) {
                arg7.put("release_build", this.h);
            }

            arg7.put("sdk_version", 1);
            arg7.put("os", "Android");
            arg7.put("os_version", Build.VERSION.RELEASE);
            arg7.put("os_api", Build.VERSION.SDK_INT);
            arg7.put("device_model", Build.MODEL);
            arg7.put("device_brand", Build.BRAND);
            arg7.put("device_manufacturer", Build.MANUFACTURER);
            arg7.put("cpu_abi", Build.CPU_ABI);
            arg7.put("build_serial", Build.SERIAL);
        } catch (Exception v2) {
        }

        try {
            DisplayMetrics v3 = arg8.getResources().getDisplayMetrics();
            v2_3 = v3.densityDpi;
            arg7.put("density_dpi", v2_3);
            switch (v2_3) {
                case 120: {
                    v2_1 = "ldpi";
                    break;
                }
                case 240: {
                    v2_1 = "hdpi";
                    break;
                }
                case 320: {
                    v2_1 = "xhdpi";
                    break;
                }
                default: {
                    v2_1 = "mdpi";
                    break;
                }
            }

            arg7.put("display_density", v2_1);
            arg7.put("resolution", v3.heightPixels + "x" + v3.widthPixels);
        } catch (Exception v2) {
        }

        try {
            v2_1 = arg8.getResources().getConfiguration().locale.getLanguage();
            if (!TextUtils.isEmpty(((CharSequence) v2_1))) {
                arg7.put("language", v2_1);
            }

            v2_3 = TimeZone.getDefault().getRawOffset() / 3600000;
            if (v2_3 >= v1) {
                v1 = v2_3;
            }

            if (v1 <= v0) {
                v0 = v1;
            }

            arg7.put("timezone", v0);
            String v0_2 = CrashUtils.c(arg8);
            if (v0_2 == null) {
            } else {
                arg7.put("access", v0_2);
            }
        } catch (Exception v0_1) {

        }
        StringBuilder v0_3 = new StringBuilder();
        try {
            if (CrashUtils.b()) {
                v0_3.append("MIUI-");
            } else if (CrashUtils.c()) {
                v0_3.append("FLYME-");
            } else {
                String v1_1 = CrashUtils.a();
                if (CrashUtils.a(v1_1)) {
                    v0_3.append("EMUI-");
                }

                if (TextUtils.isEmpty(((CharSequence) v1_1))) {
                } else {
                    v0_3.append(v1_1).append("-");
                }
            }

            v0_3.append(Build.VERSION.INCREMENTAL);
            if (v0_3.length() <= 0) {
                return;
            }

            arg7.put("rom", v0_3.toString());
        } catch (Throwable v0_4) {
        }
    }

    private Object a(String arg3) {
        Object v0 = null;
        try {
            if (!this.g.containsKey(arg3)) {
                return v0;
            }

            v0 = this.g.get(arg3);
        } catch (Exception v1) {
        }

        return v0;
    }

    final void b(JSONObject arg9, Context arg10) {
        Map v4;
        int v7 = 4;
        try {
            this.a(arg9, arg10);
            String[] v2 = new String[]{"channel", "app_version", "device_id", "openudid", "udid"};
            String[] v3 = new String[]{"version_code", "manifest_version_code", "aid", "update_version_code"};
            v4 = com.ss.android.crash.log.j.j$a.a();
            int v1;
            for (v1 = 0; v1 < 5; ++v1) {
                String v5 = v2[v1];
                if (v4.containsKey(v5)) {
                    arg9.put(v5, v4.get(v5));
                }
            }

            v1 = 0;
            while (true) {
                if (v1 >= v7) {
                    break;
                }

                String v2_1 = v3[v1];
                if (!v4.containsKey(v2_1)) {
                    ++v1;
                    continue;
                }

                try {
                    arg9.put(v2_1, Integer.parseInt((String) v4.get(v2_1)));
                    ++v1;
                    continue;
                } catch (Exception v0_1) {
                    try {
                        arg9.put(v2_1, v4.get(v2_1));
                        ++v1;
                        continue;
                    } catch (Throwable v0) {
                        return;
                    }
                }
            }
        } catch (Throwable v0) {
            return;
        }
        try {
            if (v4.containsKey("iid")) {
                arg9.put("udid", v4.get("iid"));
            }

            if (v4.containsKey("version_name")) {
                arg9.put("app_version", v4.get("version_name"));
            }
            return;
        } catch (Throwable v0) {
        }
    }
}

